require 'socket'

$files = {}

alias sribi_require require
def require(file)
     $files[file + ".rb"] = File.open(file + ".rb", "r").mtime if File.exists?(file + ".rb")
     sribi_require file
end

require 'parse/general_parse'
require 'server'
require 'bot'
require 'commands'


Thread.abort_on_exception = true

class Sribi
     
     def self.version
          return "Simple Ruby IRC Bot Interface v3.2.9"
     end
     
     def self.check_mtime(filename, mtime)
          return if File.open(filename, "r").mtime == mtime
          begin
               load filename
          rescue Object => ex
               puts "--Reload FAILED for #{filename}!"
               puts ex
               return
          end
          $files[filename] = File.open(filename, "r").mtime
          puts "--Reloaded #{filename}"
     end
     def self.begin_reload_loop
          Thread.new {
               loop {
                    $files.each do |file, mtime|
                         check_mtime file, mtime
                    end
                    sleep 10
               }
          }
     end
     @@bots = []
     @@command_handlers = []
     #If passed false, does not connect to the server; instead, it simply checks syntax.
     def self.start(opts = {})
          if $started
               return
          end
          connect = (opts[:connect] ? opts[:connect] : true)
          reload = (opts[:reload] ? opts[:reload] : true)
          $started = true
          $files[$0] = File.open($0, "r").mtime
          @@bots.each do |bot|
               if bot.commands and @@command_handlers.include? bot.commands.class
                    bot.commands.class.bot(bot)
               elsif bot.commands(nil, :privmsg) and @@command_handlers.include? bot.commands(nil, :privmsg).class
                    bot.commands(nil, :privmsg).class.bot(bot)
               else
                    @@bots.delete bot
               end
          end
          @@command_handlers.each do |cmd|
               if cmd.associated_bot and Object.const_defined? cmd.associated_bot.to_s
                    cmd.user_methods = cmd.methods(false)
               end
          end
          @@bots.map! do |bot|
               if connect
                    new_bot = bot.new
                    chan = bot.commands(nil, :channel)
                    chan.class.bot(new_bot) if chan
                    pm = bot.commands(nil, :privmsg)
                    pm.class.bot(new_bot) if pm
               end
               new_bot
          end
          begin_reload_loop if reload
          loop {
               begin
               rescue Object => ex
                    puts ex
                    @@bots.each do |bot|
                         bot.on_error(ex)
                    end
               end
          }
     end
     def self.add_bot(bot)
          @@bots << bot
     end
     def self.add_commands(cmd)
          @@command_handlers << cmd
     end
     
     
end

